#! /usr/bin/perl -w
# sysinfocheck.pl

use strict;

#####################################################
# get_eth0macinfo()
# get eth0mac info from /etc/nas/eth0mac file
#####################################################
sub get_eth0macinfo
{
  my ( %eth0mac, $eth0mac, $line );

  $eth0mac = '/etc/nas/eth0mac';
  
  # init the eth0mac
  $eth0mac{"eth0mac"} = "FFFFFFFFFFFF";

  if( ! open(ETH0MAC, $eth0mac) ){ 
    print "Coundn't open the $eth0mac file!\n";
    return \%eth0mac;
  }

  while( $line = <ETH0MAC> ){
    if( $line =~ /^\s*(\S+)\s*/){
      $eth0mac{"eth0mac"} = $1;
    }
  }

  return \%eth0mac;
}

#####################################################
# get_sysinfo()
# get sysinfo from /systeminfo file and return it
#####################################################
sub get_sysinfo
{
  my ( %sysinfo, $sysinfo, $line );

  $sysinfo = '/sysvol/etc/sysinfo';
  
  # init the %sysinfo
  $sysinfo{"MAC0"} = "000000000000";
  $sysinfo{"MAC1"} = "000000000000";
  $sysinfo{"MAC2"} = "000000000000";

  if( ! open(SYSINFO, $sysinfo) ){ 
    print "Coundn't open the $sysinfo file!\n";
    return \%sysinfo;
  }

  while( $line = <SYSINFO> ){
    if( $line =~ /^\s*MAC0\s*=\s*(\S+)\s*/){
      $sysinfo{"MAC0"} = $1;
      next;
    }elsif( $line =~ /^\s*MAC1\s*=\s*(\S+)\s*/){
      $sysinfo{"MAC1"} = $1;
      next;
    }elsif( $line =~ /^\s*MAC2\s*=\s*(\S+)\s*/){
      $sysinfo{"MAC2"} = $1;
      next;
    }
  }

  return \%sysinfo;
}

##################################################
# check_sysinfo()
# check sysinfo and reboot if not mathched
##################################################
sub check_sysinfo
{
  my ( $eth0mac, $sysinfo );

  $eth0mac = get_eth0macinfo();
  $sysinfo = get_sysinfo();

  if( $eth0mac->{"eth0mac"} eq $sysinfo->{"MAC0"} ||
      $eth0mac->{"eth0mac"} eq $sysinfo->{"MAC1"} ||
      $eth0mac->{"eth0mac"} eq $sysinfo->{"MAC2"} ){
    return 0;
  }else{
    return 1;
  }
  return 0; 
}

##################################################
# main()
# do something...
##################################################
if( check_sysinfo() == 1 ){
#`reboot`;
}
else {
  print 'nas sysinfo check ok!\n';
}
